package goods

import (
	"context"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"goods_bff/controller"
	"goods_bff/forms"
	"goods_bff/goods_proto"
	"goods_bff/pkg"
	"net/http"
	"strconv"
)

func List(c *gin.Context) {
	request := &goods_proto.GoodsFilterRequest{}
	priceMin := c.Query("price_min")
	priceMinInt, _ := strconv.Atoi(priceMin)
	priceMax := c.Query("price_max")
	priceMaxInt, _ := strconv.Atoi(priceMax)
	isHot := c.Query("is_hot")
	if isHot == "1" {
		request.IsHot = true
	}
	isNew := c.Query("is_new")
	if isNew == "1" {
		request.IsNew = true
	}
	isTab := c.Query("is_tab")
	if isTab == "1" {
		request.IsTab = true
	}
	categoryId := c.Query("category_d")
	categoryIdInt, _ := strconv.Atoi(categoryId)
	page := c.Query("page")
	pageInt, _ := strconv.Atoi(page)
	limit := c.Query("limit")
	limitInt, _ := strconv.Atoi(limit)
	keywords := c.Query("keywords")
	brandId := c.Query("brandId")
	brandIdInt, _ := strconv.Atoi(brandId)
	comment := pkg.Comment()
	list, err := comment.GoodsList(context.Background(), &goods_proto.GoodsFilterRequest{
		PriceMin:    int32(priceMinInt),
		PriceMax:    int32(priceMaxInt),
		IsHot:       request.IsHot,
		IsNew:       request.IsNew,
		IsTab:       request.IsTab,
		TopCategory: int32(categoryIdInt),
		Pages:       int32(pageInt),
		PagePerNums: int32(limitInt),
		KeyWords:    keywords,
		Brand:       int32(brandIdInt),
	})
	if err != nil {
		zap.S().Errorw("[List] 查询 【商品列表】失败")
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	reMap := map[string]interface{}{
		"total": list.Total,
	}

	goodsList := make([]interface{}, 0)
	for _, value := range list.Data {
		goodsList = append(goodsList, map[string]interface{}{
			"id":          value.Id,
			"name":        value.Name,
			"goods_brief": value.GoodsBrief,
			"desc":        value.GoodsDesc,
			"ship_free":   value.ShipFree,
			"images":      value.Images,
			"desc_images": value.DescImages,
			"front_image": value.GoodsFrontImage,
			"shop_price":  value.ShopPrice,
			"category": map[string]interface{}{
				"id":   value.Category.Id,
				"name": value.Category.Name,
			},
			"brand": map[string]interface{}{
				"id":   value.Brand.Id,
				"name": value.Brand.Name,
				"logo": value.Brand.Logo,
			},
			"is_hot":  value.IsHot,
			"is_new":  value.IsNew,
			"on_sale": value.OnSale,
		})
	}
	reMap["data"] = goodsList
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": reMap,
	})
}

func Detail(c *gin.Context) {
	id := c.Query("id")
	idInt, _ := strconv.Atoi(id)
	comment := pkg.Comment()
	r, err := comment.GetGoodsDetail(context.Background(), &goods_proto.GoodInfoRequest{Id: int32(idInt)})

	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}

	rsp := map[string]interface{}{
		"id":          r.Id,
		"name":        r.Name,
		"goods_brief": r.GoodsBrief,
		"desc":        r.GoodsDesc,
		"ship_free":   r.ShipFree,
		"images":      r.Images,
		"desc_images": r.DescImages,
		"front_image": r.GoodsFrontImage,
		"shop_price":  r.ShopPrice,
		"ctegory": map[string]interface{}{
			"id":   r.Category.Id,
			"name": r.Category.Name,
		},
		"brand": map[string]interface{}{
			"id":   r.Brand.Id,
			"name": r.Brand.Name,
			"logo": r.Brand.Logo,
		},
		"is_hot":  r.IsHot,
		"is_new":  r.IsNew,
		"on_sale": r.OnSale,
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": rsp,
	})
}

func Add(c *gin.Context) {
	goodsForm := forms.GoodsForm{}
	if err := c.ShouldBind(&goodsForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	comment := pkg.Comment()
	goods, err := comment.CreateGoods(context.Background(), &goods_proto.CreateGoodsInfo{
		Name:            goodsForm.Name,
		GoodsSn:         goodsForm.GoodsSn,
		Stocks:          goodsForm.Stocks,
		MarketPrice:     goodsForm.MarketPrice,
		ShopPrice:       goodsForm.ShopPrice,
		GoodsBrief:      goodsForm.GoodsBrief,
		Images:          goodsForm.Images,
		DescImages:      goodsForm.DescImages,
		GoodsFrontImage: goodsForm.FrontImage,
		CategoryId:      goodsForm.CategoryId,
		BrandId:         goodsForm.Brand,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": goods,
	})
}
